package com.zhou.test.spring.secrity.controller.openapi;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.google.code.kaptcha.Producer;
import com.zhou.common.lib.util.IoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 验证码操作处理
 *
 * @author ruoyi
 */
@Api(tags = "验证码模块")
@ApiSupport(order = 1)
@Slf4j
@RestController
public class CaptchaController {

    @Resource(name = "captchaProducer")
    private Producer captchaProducer;

    @Resource(name = "captchaProducerMath")
    private Producer captchaProducerMath;


//    /**
//     * 生成验证码
//     */
//    @ApiOperationSupport(order = 1)
//    @ApiOperation(value = "生成验证码")
//    @GetMapping("/captchaImage")
//    public Response<CaptchaVo> getCode(HttpServletResponse response) throws IOException {
//        boolean captchaEnabled = configService.selectCaptchaEnabled();
//        CaptchaVo captchaVo = new CaptchaVo();
//        captchaVo.setCaptchaEnabled(captchaEnabled);
//        if (!captchaEnabled) {
//            return Response.success(captchaVo);
//        }
//        // 保存验证码信息
//        String uuid = UUID.randomUUID().toString();
//        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
//
//        String capStr = null, code = null;
//        BufferedImage image = null;
//
//        // 生成验证码
//        String captchaType = MySystemProperties.getCaptchaType();
//        if ("math".equals(captchaType)) {
//            String capText = captchaProducerMath.createText();
//            capStr = capText.substring(0, capText.lastIndexOf("@"));
//            code = capText.substring(capText.lastIndexOf("@") + 1);
//            image = captchaProducerMath.createImage(capStr);
//        } else if ("char".equals(captchaType)) {
//            capStr = code = captchaProducer.createText();
//            image = captchaProducer.createImage(capStr);
//        }
//
//        redisCache.setCacheObject(verifyKey, code, MySystemProperties.getCaptchaExpiration(), TimeUnit.MINUTES);
//        // 转换流信息写出
//        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
//        try {
//            ImageIO.write(image, "jpg", os);
//        } catch (IOException e) {
//            return Response.error(e.getMessage());
//        }
//
//        captchaVo.setUuid(uuid);
//        captchaVo.setImg(Base64.encode(os.toByteArray()));
//        return Response.success(captchaVo);
//    }

    /**
     * 生成验证码
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "直接生成验证码")
    @GetMapping("/captchaImage/direct")
    public void getCodeDirect(HttpServletResponse response) throws IOException {

        String capStr = null, code = null;
        BufferedImage image = null;
        // 生成验证码
        String captchaType = "math";
        if ("math".equals(captchaType)) {
            String capText = captchaProducerMath.createText();
            capStr = capText.substring(0, capText.lastIndexOf("@"));
            code = capText.substring(capText.lastIndexOf("@") + 1);
            image = captchaProducerMath.createImage(capStr);
        } else if ("char".equals(captchaType)) {
            capStr = code = captchaProducer.createText();
            image = captchaProducer.createImage(capStr);
        }

        // 转换流信息写出
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        try {
            ImageIO.write(image, "jpg", os);
        } catch (IOException e) {
            log.error("", e);
        }
        IoUtils.writeToResponse(os.toByteArray(), response, MediaType.IMAGE_JPEG_VALUE, null);
    }
}
